package com.orangelife.mobile.shoppingcart.activity;

import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.curry.android.http.VolleyHttpUtil;
import com.curry.android.util.DialogHelper;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.orangelife.mobile.shoppingcart.adapter.ShoppingCartAdapter;
import com.orangelife.mobile.shoppingcart.bean.ShoppingCartBean;
import com.orangelife.mobile.shoppingcart.biz.ShoppingCartBiz;
import com.orangelife.mobile.shoppingcart.biz.ShoppingCartHttpBiz;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 购物车模块
 *
 * @author 绯若虚无
 */
public class ShoppingCartActivity extends OrangeLifeBaseActivity {
    /** 商品列表 */
    @Bind(R.id.expandableListView)
    ExpandableListView expandableListView;
    /** 选择全部按钮 */
    @Bind(R.id.ivSelectAll)
    ImageView ivSelectAll;
    /** 结算按钮 */
    @Bind(R.id.btnSettle)
    TextView btnSettle;
    /** 总价文本 */
    @Bind(R.id.tvCountMoney)
    TextView tvCountMoney;
    /** 标题 */
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.rlShoppingCartEmpty)
    RelativeLayout rlShoppingCartEmpty;
    @Bind(R.id.rlBottomBar)
    RelativeLayout rlBottomBar;
    @Bind(R.id.ib_title_left)
    LinearLayout ib_title_left;
    private Dialog dialog = null;

    /** 商品列表数据 */
    private List<ShoppingCartBean> mListChildGoods = new ArrayList<>();
    /** 商品列表适配器 */
    private ShoppingCartAdapter adapter;

    public void onBackClick(View view) {
        ShoppingCartActivity.this.finish();
    }

    @OnClick(R.id.ib_title_left)
    public void onClick(View v) {
        ShoppingCartActivity.this.finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shopping_cart);
        ButterKnife.bind(this);
        //request();
        initView();
        setAdapter();
    }

    @Override
    protected void onResume(){
        super.onResume();
        request();
    }


    private void setAdapter() {
        adapter = new ShoppingCartAdapter(this, mListChildGoods);
        expandableListView.setAdapter(adapter);
        adapter.setImageViewSelectAll(ivSelectAll, btnSettle, tvCountMoney, tvTitle, rlShoppingCartEmpty, rlBottomBar, expandableListView);
    }

    private void initView() {
        expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {
                return true;
            }
        });
    }

    private void request() {
        List<String> list = ShoppingCartBiz.getAllProductID();
        if (list.size() == 0) {
            showEmpty(true);
            cleanListView();
            return;
        }
        dialog = DialogHelper.getInstance().createLoadingDialog(this, "正在请求数据");
        dialog.show();
        ShoppingCartHttpBiz.requestOrderList(list, new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int errCode) {
                if (dialog != null) {
                    dialog.dismiss();
                }
                mListChildGoods = ShoppingCartHttpBiz.handleOrderList(response);
                if (mListChildGoods != null && mListChildGoods.size() != 0) {
                    adapter.setNetworkStatus(true);
                    showEmpty(false);
                    test(mListChildGoods);
                    ShoppingCartBiz.updateShopList(mListChildGoods);
                    updateListView();
                } else {
                    adapter.setNetworkStatus(false);
                    showEmpty(true);
                }
            }
        });
    }

    private void showEmpty(boolean isEmpty) {
        if (isEmpty) {
            rlShoppingCartEmpty.setVisibility(View.VISIBLE);
            rlBottomBar.setVisibility(View.GONE);
        } else {
            rlShoppingCartEmpty.setVisibility(View.GONE);
            rlBottomBar.setVisibility(View.VISIBLE);
        }
    }

    private void test(List<ShoppingCartBean> list) {
        ArrayList<ShoppingCartBean.Goods> listInvalid = new ArrayList<>();

        for (int i = list.size() - 1; i >= 0; i--) {
            //是否存在有效的项
            boolean isContainValidItem = false;
            //默认所有组都是关的，不显示，收缩
            list.get(i).setIsAllGoodsInvalid(true);
            for (int j = list.get(i).getGoods().size() - 1; j >= 0; j--) {
                String status = list.get(i).getGoods().get(j).getStatus();
                //测试，加个失效；(i == 0 && j == 0) ||
                if (status.equals(ShoppingCartBean.GOOD_INVALID)) {//如果商品已经失效，则加入失效列表
                    ShoppingCartBean.Goods goods = list.get(i).getGoods().get(j);
                    ShoppingCartBean.Goods goodsCopy = new ShoppingCartBean.Goods();
                    goodsCopy.setGoodsName(goods.getGoodsName());
                    goodsCopy.setNumber(goods.getNumber());
                    goodsCopy.setPdtDesc(goods.getPdtDesc());
                    goodsCopy.setProductID(goods.getProductID());
                    goodsCopy.setSellPloyID(goods.getSellPloyID());
                    goodsCopy.setGoodsLogo(goods.getGoodsLogo());
                    goodsCopy.setGoodsID(goods.getGoodsID());
                    goodsCopy.setIsInvalidItem(true);
                    goodsCopy.setIsBelongInvalidList(true);
                    listInvalid.add(goodsCopy);
                    list.get(i).getGoods().remove(j);
                } else {
                    //只要有一个是有效商品，取消默认隐藏组部策略
                    isContainValidItem = true;
                    list.get(i).getGoods().get(j).setIsInvalidItem(false);
                    list.get(i).getGoods().get(j).setIsBelongInvalidList(false);
                }
            }
            if (isContainValidItem) {
                list.get(i).setIsAllGoodsInvalid(false);
            } else {
                list.remove(i);
            }
        }

        if (listInvalid.size() > 0) {
            //将失效列表加入数据列表尾部；
            ShoppingCartBean scb = new ShoppingCartBean();
            scb.setIsInvalidList(true);
            scb.setGoods(listInvalid);
            list.add(scb);
        }
    }

    private void updateListView() {
        adapter.setList(mListChildGoods);
        adapter.notifyDataSetChanged();
        expandAllGroup();
    }
    private void cleanListView() {
        adapter.setTitle();
        adapter.removeAllItems();
    }

    /**
     * 展开所有组
     */
    private void expandAllGroup() {
        if (mListChildGoods != null) {
            for (int i = 0; i < mListChildGoods.size(); i++) {
                //如果是正常列表，子项全部失效，则隐藏。如果是
                if (!mListChildGoods.get(i).isAllGoodsInvalid()) {
                    expandableListView.expandGroup(i);
                }
            }
        }
    }

}
